<?php
declare(strict_types = 1);

use Swoole\Coroutine\Http\Server;
use IMOcean\Closure\Closure;
use IMOcean\Server\Coroutine\Swoole as Dispatcher;

require __DIR__ . '/../../vendor/autoload.php';

// config
$config = [
    'host' => '127.0.0.1',
    'port' => 9501,
    'ssl' => false,
];

// start a coroutine http program
$handler = new Closure(function ($host, $port, $ssl) {
    $server = new Server($host, $port, $ssl);

    // root
    $server->handle('/', function ($request, $response) {
        $response->end('root directory');
    });

    // test
    $server->handle('/test', function ($request, $response) {
        $response->end('test directory');
    });

    // stop program
    $server->handle('/stop', function ($request, $response) use ($server){
        $response->end('stop');

        $server->shutdown();
    });

    $server->start();
}, array_values($config));

// turn on
$result = Dispatcher::execute($handler);

var_dump($result);



